import 'package:flutter/material.dart';
import 'package:t_flutter_test/utils/toast.dart';

class File09HomeSearch extends StatefulWidget {
  const File09HomeSearch({super.key});

  @override
  File09HomeSearchState createState() => File09HomeSearchState();
}

class File09HomeSearchState extends State<File09HomeSearch> {
  final TextEditingController _searchController = TextEditingController();
  bool _showClear = false;

  @override
  void initState() {
    super.initState();
    _searchController.addListener(() {
      setState(() {
        _showClear = _searchController.text.isNotEmpty;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      color: Colors.grey[200],
      padding: EdgeInsets.all(10),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 10),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(15),
        ),
        child: Row(
          spacing: 6,
          children: [
            Icon(Icons.search, color: Colors.grey[600], size: 18),
            Expanded(
              child: TextField(
                controller: _searchController,
                cursorColor: Colors.grey[600], // 将光标改为灰色
                decoration: InputDecoration(
                  hintText: '搜索你要找的商品',
                  hintStyle: TextStyle(color: Colors.grey, fontSize: 14),
                  border: InputBorder.none, // 去掉默认的边框
                  contentPadding: EdgeInsets.zero, // 去掉默认的padding
                  isDense: true, // 高度是否紧凑
                ),
                style: TextStyle(fontSize: 14),
                onSubmitted: (value) {
                  // 实际搜索逻辑
                  if (value.isNotEmpty) {
                    showToast(
                      context,
                      '搜索商品: $value',
                      position: Position.center,
                    );
                  }
                },
              ),
            ),
            if (_showClear)
              GestureDetector(
                onTap: () {
                  _searchController.clear();
                },
                child: Icon(Icons.close, color: Colors.grey[600], size: 18),
              ),
          ],
        ),
        // Row(
        //   spacing: 4,
        //   children: [
        //     Icon(Icons.search, color: Colors.grey[600], size: 18),
        //     Text(
        //       '搜索你要找的商品',
        //       style: TextStyle(color: Colors.grey[600], fontSize: 14),
        //     ),
        //   ],
        // ),
      ),
    );
  }
}
